// pages/goods/goods.js
import {Goods} from 'goods-model.js';
import { Cart } from "../cart/cart-model";
var goods = new Goods();
var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked:[],
    checkGoods:null,
    number:1,
    open:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this._LoadData(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: "商品详情",
    });
  },

  _LoadData(id){
    goods.getGoods(id, res =>{
        this.setData({
          goodsArray:res,
          checkGoods:res.goods_value[0],
          cartTotalCounts:cart.getCartTotalCounts(),
        });
        this._LoadsetChecked(res.goods_value[0].value_id);
    })
  },
  _LoadsetChecked(ids){
      var array = ids.split('_');
      if(array.length > 0){
        this.setData({
          checked:array
        })
      }
  },
//点击规格
addValue(event){
   var index = goods.getDataSet(event,'index');
   var value_id = goods.getDataSet(event,'value');
   var array = this.data.checked;
   array[index] = value_id;
   this.setData({
     checked:array
   });
   var str = array.join('_');
   if(str != this.data.checkGoods.value_id){
      var objArrat = this.data.goodsArray.goods_value;
      for (let key = 0; key < objArrat.length;key++) {
        if(objArrat[key].value_id == str){
            this.setData({
              checkGoods:objArrat[key]
            });
            break;
        }
      }
   }
},

// 减少商品数量
 loadGoods(){
    if(this.data.number > 1){
       var number = this.data.number-1;
       this.setData({
         number:number
       })
    }
 },

 //增加商品
 addGoods(){
   if(this.data.number < this.data.checkGoods.store_count){
    var number = this.data.number+1;
    this.setData({
      number:number
    })
   }
 },

 //关闭
 off(){
    this.setData({
      open:false
    })
 },
 //打开
 on(){
  this.setData({
    open:true
  })
 },

 //添加进购物车
 setCart(){
   if(this.data.userArray != false){
    var goods = this.data.checkGoods;
    var obj ={
      id:goods.id,
      goods_icon:this.data.goodsArray.goods_icon,
      goods_id:goods.goods_id,
      goods_name:this.data.goodsArray.goods_name,
      value_name:goods.value_name,
      price:goods.price,
      counts:this.data.number,
      checkStatus:true,
    }
    cart.add(obj,obj.counts);
    var conts = this.data.cartTotalCounts + this.data.number;
    this.setData({
      cartTotalCounts : conts
    });
  }else{
    wx.navigateTo({
      url: '../login/login?id='+this.data.goodsArray.id,
    });
  }
 },

 //前往购物车
 gotoCart(){
  wx.switchTab({
    url: '/pages/cart/cart',
  });
 },
  /**
   * 
   */





   /**
    * 
    */

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
         userArray:goods.checkUser()
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})